package com.dbgs.insurance.insuredperson.utils;

import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class TextUntil {

    public static boolean isValidate(String... str) {
        for (int i = 0; i < str.length; i++) {
            if (str[i] == null || "".equals(str[i]) || "null".equals(str[i]) || "[null]".equals(str[i]) || "[]".equals(str[i])) {//(str[i].trim())) {
                return false;
            }
        }
        return true;
    }

    public static boolean isIntege(Integer intstr) {
        if (intstr == null) {
            return true;
        }
        return false;
    }

    public static String getEditText(EditText editText) {
        return editText.getText().toString().trim();
    }

    public static boolean isNullEditText(EditText editText) {
        String text = editText.getText().toString().trim();
        if (text != null && text.length() > 0) {
            return false;
        }
        return true;
    }

    public static boolean checkPassword(char[] passwordChars) {
        boolean mReturn = false;
        char[] str = "0123456789".toCharArray();       //不超过20位的数字组合
        char[] str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();   //由字母、数字组成，不超过20位
        char[] str2 = "abcdefghijklmnopqrstuvwxyz".toCharArray();         //由字母不超过20位
        char[] str3 = "*!~`@#$%^&()-_+={}[]|:;“'‘<,.>?/、".toCharArray();
        boolean isNumber = false;    //定义一个boolean值，用来表示是否包含数字
        boolean isA = false;
        boolean isa = false;
        boolean isfuhao = false;
        int i = 0;
        for (char a : passwordChars) {
            if (!isNumber && isCharInList(a, str)) {
                isNumber = true;
                i++;
            } else if (!isA && isCharInList(a, str1)) {
                isA = true;
                i++;
            } else if (!isa && isCharInList(a, str2)) {
                isa = true;
                i++;
            } else if (!isfuhao && isCharInList(a, str3)) {
                isfuhao = true;
                i++;
            }
            if (i >= 2) {
                mReturn = true;
                break;
            }
        }
        for (int n = 0; n < passwordChars.length; n++) {
            passwordChars[n] = 0;
        }
        return mReturn;
    }

    private static boolean isCharInList(char mChar, char[] mString) {
        for (char item : mString) {
            if (item == mChar) return true;
        }
        return false;
    }

    public static Integer getInteger(String intString) {
        if (intString == null) return null;
        Integer mReturn;
        try {
            float value =  Float.parseFloat(intString);
            mReturn = (int)value;
        } catch (Exception e) {
            mReturn = 0;
        }
        return mReturn;
    }

    public static Double getDouble(String intString) {
        if (intString == null) return null;
        Double mReturn;
        try {
            mReturn = Double.parseDouble(intString);
        } catch (Exception e) {
            mReturn = 0d;
        }
        return mReturn;
    }

    /**
     * 长照险专用--多选返回的布尔数组转换成被选中数字集合字串，逗号间隔
     *
     * @param array 多选布尔数组
     * @return 集合字串
     */
    public static String multiChoiceToString(boolean[] array) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (!array[i]) continue;
            stringBuilder.append(",");
            stringBuilder.append(i + 1);
        }
        if (stringBuilder.length() > 0) stringBuilder.deleteCharAt(0);
        String str = stringBuilder.toString();
        stringBuilder.delete(0, stringBuilder.length());
        stringBuilder = null;
        return str;
    }

    /**
     * 长照险专用--multiChoiceToString的逆向方法
     *
     * @param str     集合字串
     * @param sumItem 选项数量
     * @return 多选布尔数组
     */
    public static boolean[] multiChoiceToBooleanArray(String str, int sumItem) {
        boolean[] mReturn = new boolean[sumItem];
        for (int i = 0; i < mReturn.length; i++) {
            mReturn[i] = false;
        }
        if (str != null && str.length() != 0) {
            String[] selectedItem = str.split(",");
            for (String itemNo : selectedItem) {
                mReturn[Integer.parseInt(itemNo) - 1] = true;
            }
        }
        return mReturn;
    }

    public static String completionNumber(float num) {
        DecimalFormat decimalFormat = new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        String p = decimalFormat.format(num);
        if (num == 0) {
            return 0 + "%";
        } else {
            return p;
        }
    }

    public static List<String> stringContent(String date) {
        String[] array = date.split("\\|");
        List<String> list = new ArrayList<String>();
        for (String str : array) {
            list.add(str);
        }
        return list;
    }

    public static List<String> stringToList(String data) {
        List<String> list = new ArrayList<String>();
        if (TextUtils.isEmpty(data)) {
            return list;
        }
        String[] array = data.split(",");
        for (String str : array) {
            list.add(str);
        }
        return list;
    }

    //字符串数组转换成String逗号隔开
    public static String toString(String[] arr) {
        String str = "";
        //2,遍历数组。将每一个数组的元素和字符串相连接。
        for (int i = 0; i < arr.length; i++) {
            //判断，不是最后一个元素，后面连接逗号，是最后一个元素，后面不连接逗号。
            if (i != arr.length - 1) {
                str = str + arr[i] + ",";
            } else {
                str = str + arr[i] + "";
            }
        }
        return str;
    }

    public static String getmAnswerStr(HashSet<Integer> set) {
        Iterator<Integer> iterator = set.iterator();
        String str = null;

        while (iterator.hasNext()) {
            str += iterator.next() + ",";
        }
        return str;
    }

    //字符串数组转换成String隔开
    public static String arrayToString(String[] arr) {
        String str = "";
        //2,遍历数组。将每一个数组的元素和字符串相连接。
        for (int i = 0; i < arr.length; i++) {
            //判断，不是最后一个元素，后面连接逗号，是最后一个元素，后面不连接逗号。
            if (i != arr.length - 1) {
                str = str + arr[i] + "|";
            } else {
                str = str + arr[i] + "";
            }
        }
        return str;
    }

    /**
     * 设置TextView的值
     * 如果输入的string是null则不进行操作
     *
     * @param v   要设值的TextView
     * @param str 要设的值
     */
    public static void setText(TextView v, String str) {
        if (str == null) return;
        v.setText(str);
    }

    public static List<String> StringToList(String stringT) {
        String[] array = stringT.split(",");
        List<String> list = new ArrayList<String>();
        for (String str : array)
            list.add(str);
        return list;
    }

    public static String listToString(List<String> list) {
        String str = null;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (!TextUtils.isEmpty(list.get(i))) {
                sb.append(list.get(i) + ",");
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        str = sb.toString();
        sb.delete(0, sb.length());
        sb = null;
        return str;
    }

    public static String getImgType(String imgPath) {
        String str = "";
        if (!TextUtils.isEmpty(imgPath)) {
            str = imgPath.substring(imgPath.lastIndexOf(".") + 1);
        }
        return str;
    }

}
